/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.documents.model.DocumentTag;
import org.crystalrange.documents.service.DocumentTagService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentTagListController extends BaseController {

    protected DocumentTagService documentTagService;
    protected DocumentTag selectedDocumentTag;
    protected List<DocumentTag> documentTags;
    protected Listbox listboxDocumentTag;
    protected Search searchDocumentTag;

    public DocumentTagService getDocumentTagService() {
        return documentTagService;
    }

    public void setDocumentTagService(DocumentTagService documentTagService) {
        this.documentTagService = documentTagService;
    }

    public List<DocumentTag> getDocumentTags() {
        return documentTags;
    }

    public void setDocumentTags(List<DocumentTag> documentTags) {
        this.documentTags = documentTags;
    }

    public DocumentTag getSelectedDocumentTag() {
        return selectedDocumentTag;
    }

    public void setSelectedDocumentTag(DocumentTag selectedDocumentTag) {
        this.selectedDocumentTag = selectedDocumentTag;
    }
    
    public void onClick$toolbarbuttonDocumentTagCreate(Event event) throws Exception {
        this.documentTagCreate();
    }
    
    public void onClick$menuitemDocumentTagCreate(Event event) throws Exception {
        this.documentTagCreate();
    }
    
    public void onClick$toolbarbuttonDocumentTagEdit(Event event) throws Exception {
        this.documentTagEdit();
    }
    
    public void onClick$menuitemDocumentTagEdit(Event event) throws Exception {
        this.documentTagEdit();
    }
    
    public void onClick$toolbarbuttonDocumentTagDelete(Event event) throws Exception {
        this.documentTagDelete();
    }
    
    public void onClick$menuitemDocumentTagDelete(Event event) throws Exception {
        this.documentTagDelete();
    }
    
    public void onClick$toolbarbuttonDocumentTagSearch(Event event) throws Exception {
        this.documentTagSearch();
    }
    
    public void onClick$menuitemDocumentTagSearch(Event event) throws Exception {
        this.documentTagSearch();
    }
    
    public void onClick$toolbarbuttonDocumentTagRefresh(Event event) throws Exception {
        this.documentTagRefresh();
    }
    
    public void onClick$menuitemDocumentTagRefresh(Event event) throws Exception {
        this.documentTagRefresh();
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentTag = new Search(DocumentTag.class);
        this.searchDocumentTag.setMaxResults(100);
        this.searchDocumentTag.addFetch("createdBy");
        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        component.setAttribute("controller", this);
    }
    
    
    private void documentTagCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/documentTag.zul");
    }
    
    private void documentTagEdit() throws Exception {
        if (this.selectedDocumentTag != null) {
            this.execution.sendRedirect("~./zul/html/documentTag.zul?Id=" + this.selectedDocumentTag.getId().toString());
        }
    }
    
    private void documentTagDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxDocumentTag.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    DocumentTag documentTag = (DocumentTag) selectedItem.getValue();
                    this.documentTagService.remove(documentTag);
                }
                this.selectedDocumentTag = null;
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        this.binder.loadAll();
    }
    
    private void documentTagSearch() throws Exception {
        this.execution.setAttribute("searchDocumentTag", this.searchDocumentTag);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentTagSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentTag = (Search) this.execution.getAttribute("searchDocumentTag");
        }

        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        this.binder.loadAll();
    }
    
    private void documentTagRefresh() throws Exception {
        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        this.binder.loadAll();
    }
}
